package com.itcast.demo_one

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.itcast.demo_one.model.LoginRequest
import com.itcast.demo_one.utils.RetrofitClient
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class NetActivity : AppCompatActivity() {
    @SuppressLint("ShowToast", "SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_net)

        // 1. 发起Get请求获取频道数据
        // 1.1 获取点击按钮
        val btnGet = findViewById<Button>(R.id.getChannel)
        // 1.2 添加点击事件
        btnGet.setOnClickListener {
            // 1.3 创建协程
            lifecycleScope.launch(context = Dispatchers.Main) {
               try {
                   // 1.3.1 切换到IO线程发起网络请求
                   val res =  withContext(Dispatchers.IO) {
                       RetrofitClient.apiService.getChannels()
                   }
                   // 1.3.2 获取数据成功，显示数据
                   if (res.code == 10000) {
                       val channels = res.data?.channels
                       // 1.3.2.1 遍历频道列表，显示数据
                       val channelText = channels?.joinToString(separator = ",") {
                           it.name
                       }
                       Toast.makeText(this@NetActivity, "频道列表：$channelText", Toast.LENGTH_SHORT).show()
                       findViewById<TextView>(R.id.channel).text = "频道列表：$channelText"
                   } else {
                       // 1.3.3 获取数据失败，显示错误信息
                       Toast.makeText(this@NetActivity, "获取频道列表失败", Toast.LENGTH_SHORT).show()

                   }
               } catch (e: Exception){
                   Toast.makeText(this@NetActivity, "获取频道列表失败", Toast.LENGTH_SHORT).show()
               }
            }
        }


        // 2. 登录操作
        findViewById<Button>(R.id.login).setOnClickListener {
            try {
                lifecycleScope.launch( context = Dispatchers.Main) {
                    val req = LoginRequest("admin", "123456")
                    val res = withContext(Dispatchers.IO){
                        RetrofitClient.apiService.login(req)
                    }

                    if (res.code == 10000){
                        val token = res.data?.token
                        findViewById<TextView>(R.id.tv).text = "登录成功, Token: $token"
                    } else {
                        // 处理响应
                        Toast.makeText(
                            this@NetActivity,
                            "登录失败",
                            Toast.LENGTH_SHORT
                        ).show()
                    }

                }
            }catch (e: Exception){
                // 处理错误
                Toast.makeText(
                    this@NetActivity,
                    "请求失败",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }

    }
}